layui.config({
  base: '/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
  murl: 'url' //如果 mymod.js 是在根目录，也可以不用设定别名
});
layui.use(['form','murl','layer','element','laydate','laytpl'], function () {
     var laydate = layui.laydate,laytpl = layui.laytpl ,form = layui.form, murl=layui.murl;
     var layerTips = parent.layer === undefined ? layui.layer : parent.layer;

      var id=murl.get("id");
      $("#id").val(id);
      $.ajax({
           url: '/admin/ordersTask/getInfo?id='+id,
           type: 'get',
           dataType: "json",
           success: function (data) {
               if(data.rel){
                 var result=data.result;
                 setLabelValues(result);

                 if(result.type==0){
                   var imgHtml=$("#imgCard").html().replace("#CARD_TITLE","订单文件").replace(" #CARD_CONTENT", " <img src='"+result.file+"' width='100%'  class='img-rounded' />");
                    imgHtml+=$("#imgCard").html().replace("#CARD_TITLE","福蛙凭证").replace(" #CARD_CONTENT", " <img src='"+result.resourcesFiles+"' width='100%'  class='img-rounded' />");
                   $("#filesObj").html(imgHtml);
                    var viewer = new Viewer(document.getElementById('filesObj'));
                 }

                  if(result.type==1){
                    var imgHtml=$("#imgCard").html().replace("#CARD_TITLE","订单文件").replace(" #CARD_CONTENT", "<audio controls><source src='"+result.file+"'type='audio/mpeg'></audio>")
                           + $("#imgCard").html().replace("#CARD_TITLE","福蛙凭证").replace(" #CARD_CONTENT", " <audio controls><source src='"+result.resourcesFiles+"'type='audio/mpeg'></audio>");
                    $("#filesObj").html(imgHtml);
                     var viewer = new Viewer(document.getElementById('filesObj'));
                  }
                  $("#status").val(result.status);


               }else{
                 layerTips.msg(data.msg);
               }
           }
       });
    form.on('submit(save)', function(data){
        if($("#status").val()==3){
           data.field.status="1";
           data.field.reviewStatus="1";
           submitForm(data);
        }

       return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
     });

     form.on('submit(cancel)', function(data){
         if($("#replay2").val()==""){
             layerTips.msg('请填写回执');
             return false;
         }
       if($("#status").val()==3){
          data.field.status="3";
          data.field.reviewStatus="2";
          submitForm(data);
       }
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
      });
});

function submitForm(data){
  $.ajax({
     url: '/admin/ordersTask/save',
     type: 'post',
     data: data.field,
     dataType: "json",
     success: function (data) {
         var layerTips = parent.layer === undefined ? layui.layer : parent.layer;
         if(data.rel){
            layerTips.msg('保存成功');
             window.setTimeout(function () {
                         parent.tab.deleteTab(parent.tab.getCurrentTabId(),true);
                     },1000);
         }else{
           layerTips.msg(data.msg);
         }
     }
   });
}

function setLabelValues(data) {
    for (var p in data) {
        if(p.indexOf("time")>=0){
           $("#" + p + "").html(data[p].split(" ")[0]);
        }else{
           $("#" + p + "").html(data[p]);
        }
    }
}

function setInputValues(data) {
    for (var p in data) {
        $("#" + p + "").val(data[p]);
    }
}
